<?php

namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\CourseCoverImg
 *
 * @property int $id
 * @property int $course_id 课程id
 * @property int $img_id 图片id
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\CourseCoverImg onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereCourseId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CourseCoverImg whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\CourseCoverImg withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\CourseCoverImg withoutTrashed()
 * @mixin \Eloquent
 */
class CourseCoverImg extends Model
{
    //
    use SoftDeletes;

    public static function getImgUrlByCourseId($course_id = 0){
        if(empty($course_id))
            return '';

        $data = '';
        $courseImgInfo = self::whereCourseId($course_id)->first();
        if($courseImgInfo){
            $data = FileManager::getImgUrl($courseImgInfo->img_id);
        }

        return $data;
    }

    public static function getCourseCoverImgList($course_id = 0,$type = 'id'){
        if(empty($course_id))
            return [];

        $data = [];
        $courseImgList = self::whereCourseId($course_id)->get();
        if(count($courseImgList) > 0){
            foreach ($courseImgList as $courseImg) {
                if($type == 'id'){
                    $imgInfo = $courseImg->img_id;
                }else{
                    $imgInfo = FileManager::getImgUrl($courseImg->img_id);
                }

                array_push($data,$imgInfo);
            }
        }

        return $data;
    }
}
